#include "dibujo2D.h"

/*------------------------------------------------------------------------------
	Constructor
------------------------------------------------------------------------------*/
CDibujo2D::CDibujo2D()
{
	m_fX=0.0;
	m_fY=0.0;
	m_fEscala=1.0;
	m_bTransparente=TRUE;
	m_aColor[0]=1.0f;
	m_aColor[1]=1.0f;
	m_aColor[2]=1.0f;
}

/*------------------------------------------------------------------------------
	Destructor
------------------------------------------------------------------------------*/
CDibujo2D::~CDibujo2D()
{
}

/*------------------------------------------------------------------------------
	Class initialization
------------------------------------------------------------------------------*/
Bool CDibujo2D::Iniciar(char *szNombreTextura,float X,float Y,float Escala,Bool transparente)
{
	m_fX=X;
	m_fY=Y;
	m_fEscala=Escala;
	m_bTransparente=transparente;
	
	if(m_Textura.Cargar(szNombreTextura,GL_RGBA))
		return TRUE;
	
	return FALSE;
}

/*------------------------------------------------------------------------------
	Frees memory
------------------------------------------------------------------------------*/
void CDibujo2D::Liberar()
{
	m_Textura.Liberar();
}

/*------------------------------------------------------------------------------
	Draws the 2D object
------------------------------------------------------------------------------*/
void CDibujo2D::Dibujar()
{
	m_Textura.Bind();	 
	glLoadIdentity();
	glDisable( GL_DEPTH_TEST );	
	
	
	if(m_bTransparente)
	{
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	}
	else
	{
		glEnable(GL_ALPHA_TEST);
		glAlphaFunc(GL_GREATER, 0.1f); 
	}

	glMatrixMode( GL_PROJECTION );
	glPushMatrix();
	glLoadIdentity( );
	glOrtho( -1, 1, -1, 1, -1, 1 );
	
	glMatrixMode( GL_MODELVIEW );
    glPushMatrix( );
    glLoadIdentity( );
    
	glTranslatef(m_fX,m_fY,0);	
	glScalef(m_fEscala,m_fEscala,1.0);
	glColor3f(m_aColor[0],m_aColor[1],m_aColor[2]);
    
   	glBegin( GL_TRIANGLES );
    	  glTexCoord2f( 0.0f,  1.0f ); glVertex3f( -1.0f, -1.0f,  0.0f );
    	  glTexCoord2f( 1.0f,  1.0f ); glVertex3f(  1.0f, -1.0f,  0.0f );
    	  glTexCoord2f( 1.0f,  0.0f ); glVertex3f(  1.0f,  1.0f,  0.0f );
      
	      glTexCoord2f( 0.0f,  1.0f ); glVertex3f( -1.0f, -1.0f,  0.0f );
    	  glTexCoord2f( 1.0f,  0.0f ); glVertex3f(  1.0f,  1.0f,  0.0f );
	      glTexCoord2f( 0.0f,  0.0f ); glVertex3f( -1.0f,  1.0f,  0.0f );
    glEnd( );
    
    glMatrixMode( GL_PROJECTION );
    glPopMatrix( );
    glMatrixMode( GL_MODELVIEW );
    glPopMatrix( );    

	glDisable(GL_ALPHA_TEST);    
    glEnable( GL_DEPTH_TEST );
    glDisable(GL_BLEND);
	
}

